#include "Mod_ForwardServer.h"


cForwardServer ForwardServer;

/**
 * 函数功能：cForwardServer构造函数
 * 参    数：无
 * 返 回 值：无
 */
cForwardServer::cForwardServer()
{
    /*设置W5500结构体指针*/
    this->pst_W5500Struct = &gst_W5500_Dev;

    /*设置Socket收发缓存指针*/
    this->pst_W5500Struct->st_Socket[0].puch_RxBuff = this->auch_Socket0RxBuff;
    this->pst_W5500Struct->st_Socket[0].puch_TxBuff = this->auch_Socket0TxBuff;

    this->pst_W5500Struct->st_Socket[1].puch_RxBuff = this->auch_Socket1RxBuff;
    this->pst_W5500Struct->st_Socket[1].puch_TxBuff = this->auch_Socket1TxBuff;

    this->pst_W5500Struct->st_Socket[2].puch_RxBuff = this->auch_Socket2RxBuff;
    this->pst_W5500Struct->st_Socket[2].puch_TxBuff = this->auch_Socket2TxBuff;

    this->pst_W5500Struct->st_Socket[3].puch_RxBuff = this->auch_Socket3RxBuff;
    this->pst_W5500Struct->st_Socket[3].puch_TxBuff = this->auch_Socket3TxBuff;

    this->pst_W5500Struct->st_Socket[5].puch_RxBuff = this->auch_Socket5RxBuff;
	  this->pst_W5500Struct->st_Socket[5].puch_TxBuff = this->auch_Socket5TxBuff;

    /*设置Socket收发字节数指针*/
    this->pst_W5500Struct->st_Socket[0].puin_RxSize = &this->uin_Socket0RxSize;
    this->pst_W5500Struct->st_Socket[0].puin_TxSize = &this->uin_Socket0TxSize;

    this->pst_W5500Struct->st_Socket[1].puin_RxSize = &this->uin_Socket1RxSize;
    this->pst_W5500Struct->st_Socket[1].puin_TxSize = &this->uin_Socket1TxSize;

    this->pst_W5500Struct->st_Socket[2].puin_RxSize = &this->uin_Socket2RxSize;
    this->pst_W5500Struct->st_Socket[2].puin_TxSize = &this->uin_Socket2TxSize;

    this->pst_W5500Struct->st_Socket[3].puin_RxSize = &this->uin_Socket3RxSize;
    this->pst_W5500Struct->st_Socket[3].puin_TxSize = &this->uin_Socket3TxSize;

    this->pst_W5500Struct->st_Socket[5].puin_RxSize = &this->uin_Socket5RxSize;
		this->pst_W5500Struct->st_Socket[5].puin_TxSize = &this->uin_Socket5TxSize;

    /*设置Socket和串口映射*/
    this->pst_Socket0Uart = &gst_Uart5;
    this->pst_Socket1Uart = &gst_Uart2;
    this->pst_Socket2Uart = &gst_Uart4;
    this->pst_Socket3Uart = &gst_Uart3;

    /*设置串口中断回调函数指针*/
    this->pst_Socket0Uart->RxCplCallback = UartRxCplCallback;
    this->pst_Socket0Uart->TxCplCallback = UartTxCplCallback;
    this->pst_Socket0Uart->ErrorCallback = UartErrorCallback;

    this->pst_Socket1Uart->RxCplCallback = UartRxCplCallback;
    this->pst_Socket1Uart->TxCplCallback = UartTxCplCallback;
    this->pst_Socket1Uart->ErrorCallback = UartErrorCallback;

    this->pst_Socket2Uart->RxCplCallback = UartRxCplCallback;
    this->pst_Socket2Uart->TxCplCallback = UartTxCplCallback;
    this->pst_Socket2Uart->ErrorCallback = UartErrorCallback;

    this->pst_Socket3Uart->RxCplCallback = UartRxCplCallback;
    this->pst_Socket3Uart->TxCplCallback = UartTxCplCallback;
    this->pst_Socket3Uart->ErrorCallback = UartErrorCallback;

    /*数据缓存区清零*/
    memset(this->auch_Socket0RxBuff, 0, BUF_SIZE);
    memset(this->auch_Socket0TxBuff, 0, BUF_SIZE);

    memset(this->auch_Socket1RxBuff, 0, BUF_SIZE);
    memset(this->auch_Socket1TxBuff, 0, BUF_SIZE);

    memset(this->auch_Socket2RxBuff, 0, BUF_SIZE);
    memset(this->auch_Socket2TxBuff, 0, BUF_SIZE);

    memset(this->auch_Socket3RxBuff, 0, BUF_SIZE);
    memset(this->auch_Socket3TxBuff, 0, BUF_SIZE);

    memset(this->auch_Socket5RxBuff, 0, BUF_SIZE);
	memset(this->auch_Socket5TxBuff, 0, BUF_SIZE);





    
}

/**
 * 函数功能：cForwardServer初始化前, 从Flash读取网络配置信息
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::SetNetConfig()
{
    //NetCfg_TypeDef localnet_config;
    memset(&localnet_config, 0x0, sizeof(NetCfg_TypeDef));

    /*从flash读取网络配置信息*/
    localnet_config = NetworkConfig_Read();

    /*如果读取到的ip地址为0, 则不进行网络配置*/
    if (localnet_config.auch_SIP[0] == 0)
    {
        return;
    }
    this->pst_W5500Struct->st_CfgMsg.auch_GA[0] = localnet_config.auch_GA[0];
    this->pst_W5500Struct->st_CfgMsg.auch_GA[1] = localnet_config.auch_GA[1];
    this->pst_W5500Struct->st_CfgMsg.auch_GA[2] = localnet_config.auch_GA[2];
    this->pst_W5500Struct->st_CfgMsg.auch_GA[3] = localnet_config.auch_GA[3];
    this->pst_W5500Struct->st_CfgMsg.auch_SIP[0] = localnet_config.auch_SIP[0];
    this->pst_W5500Struct->st_CfgMsg.auch_SIP[1] = localnet_config.auch_SIP[1];
    this->pst_W5500Struct->st_CfgMsg.auch_SIP[2] = localnet_config.auch_SIP[2];
    this->pst_W5500Struct->st_CfgMsg.auch_SIP[3] = localnet_config.auch_SIP[3];
    this->pst_W5500Struct->st_CfgMsg.auch_SUB[0] = localnet_config.auch_SUB[0];
    this->pst_W5500Struct->st_CfgMsg.auch_SUB[1] = localnet_config.auch_SUB[1];
    this->pst_W5500Struct->st_CfgMsg.auch_SUB[2] = localnet_config.auch_SUB[2];
    this->pst_W5500Struct->st_CfgMsg.auch_SUB[3] = localnet_config.auch_SUB[3];		
		

    // auch_Sock_ServerIP
    /*从flash读取网络配置信息*/
    socket_config = SocketCfg_Read();

    /*赋值给全局变量方便后期调用*/
    auch_Sock_ServerIP[0] = socket_config.server_ip[0];
    auch_Sock_ServerIP[1] = socket_config.server_ip[1];
    auch_Sock_ServerIP[2] = socket_config.server_ip[2];
    auch_Sock_ServerIP[3] = socket_config.server_ip[3];

    uin_Sock0ServerPort = socket_config.port0;
    uin_Sock1ServerPort = socket_config.port1;
    uin_Sock2ServerPort = socket_config.port2;
    uin_Sock3ServerPort = socket_config.port3;
	uin_Sock4ServerPort = socket_config.port4;
    uin_Sock5ServerPort = socket_config.port5;		
		
	this->pst_W5500Struct->st_Socket[0].uin_Port = socket_config.localport0;
	this->pst_W5500Struct->st_Socket[1].uin_Port = socket_config.localport1;
	this->pst_W5500Struct->st_Socket[2].uin_Port = socket_config.localport2;
	this->pst_W5500Struct->st_Socket[3].uin_Port = socket_config.localport3;
	this->pst_W5500Struct->st_Socket[4].uin_Port = socket_config.localport4;
	this->pst_W5500Struct->st_Socket[5].uin_Port = socket_config.localport5;


    /* 从 Flash 读取串口配置信息 */
    if (!UartConfig_ReadAll(g_uartConfigs))
    {
        printf("串口配置读取失败，加载默认配置\r\n");    
        for (int i = 0; i < UART_PORT_NUM; i++)
        {
            g_uartConfigs[i] = g_defaultUartConfig;
        }
    }
    /* 应用配置 */
    this->pst_Socket0Uart->config = g_uartConfigs[0];
    this->pst_Socket1Uart->config = g_uartConfigs[1];
    this->pst_Socket2Uart->config = g_uartConfigs[2];
    this->pst_Socket3Uart->config = g_uartConfigs[3];
    //this->pst_Socket4Uart->config = g_uartConfigs[4];
    //this->pst_Socket5Uart->config = g_uartConfigs[4];

}

/**
 * 函数功能：cForwardServer初始化
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::Init(void)
{
    //reset
    Drv_W5500_Reset(this->pst_W5500Struct);

    /*从flash读取网络配置信息,然后初始化*/
    SetNetConfig();
    /*初始化W5500*/
    Drv_W5500_Init(this->pst_W5500Struct);

    // 初始化 UART 接收环形缓冲区
    circle_buf_init(&Uart0RxCircleBuf, BUF_SIZE, auch_Uart0RxCircleBuff);
    circle_buf_init(&Uart1RxCircleBuf, BUF_SIZE, auch_Uart1RxCircleBuff);
    circle_buf_init(&Uart2RxCircleBuf, BUF_SIZE, auch_Uart2RxCircleBuff);
    circle_buf_init(&Uart3RxCircleBuf, BUF_SIZE, auch_Uart3RxCircleBuff);

    // 初始化时设置串口波特率 ,波特率应该从配置文件中读取
    // 设置波特率时不能重置调试口uart1的，否则会使网页不能正常打开
    STM32UartSetConfig(this->pst_Socket0Uart);
    STM32UartSetConfig(this->pst_Socket1Uart);
    STM32UartSetConfig(this->pst_Socket2Uart);
    STM32UartSetConfig(this->pst_Socket3Uart);

    /*开启串口接收*/
    // STM32UartReceive(this->pst_Socket0Uart, auch_Uart0RxBuff, BUF_SIZE);
    STM32UartReceive(this->pst_Socket1Uart, auch_Socket1TxBuff, BUF_SIZE);
    STM32UartReceive(this->pst_Socket2Uart, auch_Uart2RxBuff, BUF_SIZE);
    STM32UartReceive(this->pst_Socket3Uart, auch_Socket3TxBuff, BUF_SIZE);

    // printf("ForwardServer_Init success\r\n");
}

/**
 * 函数功能：处理网卡和串口互相转发
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::Process(void)
{
    this->TCPToUart();
    this->UartToTCP();
	  this->HeartToSocket();
}

void cForwardServer::CheckNetwork(void)
{
    static uint32_t lastCheckTime = 0;
    uint32_t currentTime = HAL_GetTick(); // 获取当前时间

    // 每隔 5 秒检测一次网络状态
    if (currentTime - lastCheckTime >= TCP_RECONNECT_TIMEOUT)
    {
        lastCheckTime = currentTime;
        Drv_W5500_PHYCheck(this->pst_W5500Struct);
    }
}

/**
 * 函数功能：处理网卡和串口互相转发
 * 参    数：无
 * 返 回 值：无
 */    
void cForwardServer::Close(void)
{        	
	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG);
	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET4_REG);
  	Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG);
	
//	  //这里将http的socket也关闭重连
//	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET6_REG);
//	  Drv_W5500_CloseSocket(this->pst_W5500Struct, W5500_SELECT_SOCKET7_REG);
}

void cForwardServer::W5500_CheckReconnect(W5500_TypeDef *pst_EthStruct, uint8_t uch_Socket, uint8_t *puch_ServerIP, uint16_t uin_ServerPort)
{
    static uint32_t lastRecvTime = 0;
    uint8_t status = Drv_W5500_GetSn_SR(pst_EthStruct, uch_Socket);
    //printf("Socket %d Status: 0x%02X\n reconnect port: %d\n", uch_Socket>>2, status, uin_ServerPort);
    // 1. 如果服务器已经断开，则尝试重连
    if (status == W5500_Sn_SR_CLOSED || status == W5500_Sn_SR_FIN_WAIT)
    {
        printf("Socket %d closed, reconnecting...\n", (uch_Socket >> 2));
        Drv_W5500_CloseSocket(pst_EthStruct, uch_Socket);
        Drv_W5500_Connect(pst_EthStruct, uch_Socket, puch_ServerIP, uin_ServerPort);
        lastRecvTime = HAL_GetTick();  // 重置超时时间
    }

    // 2. 长时间未收到数据，认为服务器可能断开，主动重连
    if ((HAL_GetTick() - lastRecvTime) > TCP_RECONNECT_TIMEOUT)
    {
        printf("Socket %d timeout, reconnecting...\n", (uch_Socket >> 2));
        Drv_W5500_CloseSocket(pst_EthStruct, uch_Socket);
        Drv_W5500_Connect(pst_EthStruct, uch_Socket, puch_ServerIP, uin_ServerPort);
        lastRecvTime = HAL_GetTick();
    }
}

/**
 * 函数功能：处理网口数据通过串口转发
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::TCPToUart(void)
{
    uint8_t auch_RecvFlag[8];    
   

    /*TCP服务端处理*/
    //Drv_TCPServer(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
    //    Drv_TCPServer(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
    //    Drv_TCPServer(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
    //    Drv_TCPServer(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG);

    // TCP客户端处理
    // Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
    // Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
    // Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG,serverIP,40001) ;

    // Drv_TCPServer(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);

    //Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG, auch_Sock_ServerIP, this->uin_Sock0ServerPort);
    Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG, auch_Sock_ServerIP, this->uin_Sock1ServerPort);
    Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG, auch_Sock_ServerIP, this->uin_Sock2ServerPort);
    Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG, auch_Sock_ServerIP, this->uin_Sock3ServerPort);
    Drv_TCPClient(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG, auch_Sock_ServerIP, this->uin_Sock5ServerPort);

    auch_RecvFlag[0] = Drv_W5500_GetRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
    auch_RecvFlag[1] = Drv_W5500_GetRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
    auch_RecvFlag[2] = Drv_W5500_GetRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
    auch_RecvFlag[3] = Drv_W5500_GetRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG);
    auch_RecvFlag[5] = Drv_W5500_GetRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG);

    /*判断Socketn是否有接收数据，如果有发送到相应串口*/
    if (auch_RecvFlag[0] != false)
    {
        STM32UartTransmit(this->pst_Socket0Uart, this->auch_Socket0RxBuff, this->uin_Socket0RxSize);
        Drv_W5500_ClearRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
    }

    if (auch_RecvFlag[1] != false)
    {
        STM32UartTransmit(this->pst_Socket1Uart, this->auch_Socket1RxBuff, this->uin_Socket1RxSize);
        Drv_W5500_ClearRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
    }
    if (auch_RecvFlag[2] != false)
    {
        STM32UartTransmit(this->pst_Socket2Uart, this->auch_Socket2RxBuff, this->uin_Socket2RxSize);
        Drv_W5500_ClearRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
    }
    if (auch_RecvFlag[3] != false)
    {
        STM32UartTransmit(this->pst_Socket3Uart, this->auch_Socket3RxBuff, this->uin_Socket3RxSize);
        Drv_W5500_ClearRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG);
    }
    if (auch_RecvFlag[5] != false)
    {
        //this->auch_Socket3RxBuff, this->uin_Socket3RxSize
        if (this->uin_Socket5RxSize > 0)
        {
            // 解析命令并控制 IO
            TCP_ProcessCommand(this->auch_Socket5RxBuff, this->uin_Socket5RxSize);
        }


        Drv_W5500_ClearRecvFlag(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG);
    }
		
		

    //W5500_CheckReconnect(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG, auch_Sock_ServerIP, this->uin_Sock5ServerPort);
}

/**
 * 函数功能：处理串口数据通过网口转发
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::UartToTCP(void)
{
	
	
		
//    if (this->UartToSocket0Flag != 0)
//    {
//        Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
//        this->UartToSocket0Flag = 0;
//    }

    if (this->UartToSocket1Flag != 0)
    {
        Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
        this->UartToSocket1Flag = 0;
    }

    Drv_W5500_SendFromRingBuffer(this->pst_W5500Struct, 
                             &Uart2RxCircleBuf, 
                             ForwardServer.auch_Socket2TxBuff, 
                             &ForwardServer.uin_Socket2TxSize, 
                             W5500_SELECT_SOCKET2_REG);
    
    Drv_W5500_SendFromRingBuffer(this->pst_W5500Struct, 
                             &Uart3RxCircleBuf, 
                             ForwardServer.auch_Socket3TxBuff, 
                             &ForwardServer.uin_Socket3TxSize, 
                             W5500_SELECT_SOCKET3_REG);   
}

/**
 * 函数功能：处理向SOcket连接发送心跳包
 * 参    数：无
 * 返 回 值：无
 */
void cForwardServer::HeartToSocket(void)
{
	  uint32_t now = HAL_GetTick();  // 获取当前时间（ms）
    
		// socket0 心跳包逻辑
//    if ((now - lastHeartbeatTick_Socket0) >= TCP_RECONNECT_TIMEOUT)
//    {
//				ForwardServer.uin_Socket0TxSize=sizeof(heartbeatPacket0);
//			  memcpy(ForwardServer.auch_Socket0TxBuff, heartbeatPacket0, sizeof(heartbeatPacket0));   			
//				Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET0_REG);
//        lastHeartbeatTick_Socket0 = now;
//    }
		// socket1 心跳包逻辑
    if ((now - lastHeartbeatTick_Socket1) >= SOCK_RECONNECT_TIMEOUT)
    {
				//ForwardServer.uin_Socket1TxSize=sizeof(heartbeatPacket1);
			  //memcpy(ForwardServer.auch_Socket1TxBuff, heartbeatPacket1, sizeof(heartbeatPacket1));   			
				//Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
			  Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
        lastHeartbeatTick_Socket1 = now;
//			  uint8_t sock1status = Drv_W5500_GetSn_SR(this->pst_W5500Struct, W5500_SELECT_SOCKET1_REG);
//			  if ((sock1status != W5500_Sn_SR_ESTABLISHED) && (sock1status != W5500_Sn_SR_CLOSE_WAIT))
//        {
//					  printf("socket1 need reconnect\n");
//        }
//        lastHeartbeatTick_Socket1 = now;
    }
		// socket2 心跳包逻辑
    if ((now - lastHeartbeatTick_Socket2) >= SOCK_RECONNECT_TIMEOUT)
    {
				//ForwardServer.uin_Socket2TxSize=sizeof(heartbeatPacket2);
			  //memcpy(ForwardServer.auch_Socket2TxBuff, heartbeatPacket2, sizeof(heartbeatPacket2));   		
				Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
        lastHeartbeatTick_Socket2 = now;
//				uint8_t sock1status = Drv_W5500_GetSn_SR(this->pst_W5500Struct, W5500_SELECT_SOCKET2_REG);
//			  if ((sock1status != W5500_Sn_SR_ESTABLISHED) && (sock1status != W5500_Sn_SR_CLOSE_WAIT))
//        {
//					  printf("socket2 need reconnect\n");
//        }
//        lastHeartbeatTick_Socket2 = now;
    }
		if ((now - lastHeartbeatTick_Socket3) >= SOCK_RECONNECT_TIMEOUT)
    {		
				Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET3_REG);
        lastHeartbeatTick_Socket3 = now;
    }
		// socket5 心跳包逻辑
    if ((now - lastHeartbeatTick_Socket5) >= SOCK_RECONNECT_TIMEOUT)
    {
				//ForwardServer.uin_Socket5TxSize=sizeof(heartbeatPacket5);
			  //memcpy(ForwardServer.auch_Socket5TxBuff, heartbeatPacket5, sizeof(heartbeatPacket5));   			
				Drv_W5500_TCPSend(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG);
        lastHeartbeatTick_Socket5 = now;
//			  uint8_t sock1status = Drv_W5500_GetSn_SR(this->pst_W5500Struct, W5500_SELECT_SOCKET5_REG);
//			  if ((sock1status != W5500_Sn_SR_ESTABLISHED) && (sock1status != W5500_Sn_SR_CLOSE_WAIT))
//        {
//					  printf("socket5 need reconnect\n");
//        }
//        lastHeartbeatTick_Socket5 = now;
    }
}

/**
 * 函数功能： 从环形缓冲区取出数据并发送到网络
 * 参    数： pst_W5500Struct 网口结构体
 *           p_RingBuf 环形缓冲区
 *           p_TxBuff 发送缓冲区
 *           p_TxSize 发送字节数地址
 *           uch_SocketReg 选择的socket
 * 返 回 值：无
 */
void Drv_W5500_SendFromRingBuffer(W5500_TypeDef *pst_W5500Struct,
                                  circle_buf *p_RingBuf,
                                  uint8_t *p_TxBuff,
                                  uint16_t *p_TxSize,
                                  uint8_t uch_SocketReg)
{
    uint8_t *txBuffPtr = p_TxBuff; // 指针指向缓冲区起始地址
    *p_TxSize = 0;

    // 从环形缓冲区读取数据到发送缓冲区
    while (circle_buf_read(p_RingBuf, txBuffPtr) == 0)
    {
        txBuffPtr++;
        (*p_TxSize)++;
    }

    // 如果有数据要发送
    if (*p_TxSize > 0)
    {
			  //printf("Sending data on socket %d, size = %d\n", uch_SocketReg>>2, *p_TxSize);
        Drv_W5500_TCPSend(pst_W5500Struct, uch_SocketReg);
        *p_TxSize = 0;
    }
}

/**
 * 函数功能：串口接收完成中断回调函数
 * 参    数：pst_UartStruct 串口结构体
 *           uin_Size 接收字节数
 * 返 回 值：无
 */
void UartRxCplCallback(struct _Uart_TypeDef *pst_UartStruct, uint16_t uin_Size)
{
    if (pst_UartStruct == ForwardServer.pst_Socket0Uart)
    {
        // 原来该处设置了收到数据标志位，检测该标志位即可将数据发送到网络
        // ForwardServer.UartToSocket0Flag = 1;
        // 原来用发送长度=接收长度来 传回tcp需要发送的字节大小，改环形缓冲区后准备弃用这个
        // ForwardServer.uin_Socket0TxSize = uin_Size;

        /// 新方法改为将接收到的数据储存到环形缓冲区，环形缓冲区有数据则自动触发发送
        // 此处uin_Size从哪传回来需要思考？？
        for (int i = 0; i < uin_Size; i++)
        {
            if (circle_buf_write(&ForwardServer.Uart0RxCircleBuf, ForwardServer.auch_Socket0TxBuff[i]) < 0)
            {
                printf("UART1 full!\n");
            }
        }

        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket0Uart, ForwardServer.auch_Socket0TxBuff, BUF_SIZE);
    }

    else if (pst_UartStruct == ForwardServer.pst_Socket1Uart)
    {
        ForwardServer.UartToSocket1Flag = 1;
        ForwardServer.uin_Socket1TxSize = uin_Size;
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket1Uart, ForwardServer.auch_Socket1TxBuff, BUF_SIZE);
    }

    else if (pst_UartStruct == ForwardServer.pst_Socket2Uart)
    {
        // ForwardServer.UartToSocket2Flag = 1;
        // ForwardServer.uin_Socket2TxSize = uin_Size;
        //  将接收到的数据存入环形缓冲区
        // uint8_t revSize= sizeof(ForwardServer.auch_Uart2RxBuff);
        // printf("uart2 revSize is %d\n",uin_Size);
        for (uint16_t i = 0; i < uin_Size; i++)
        {
            // printf("start write circle,the  %u time val is %c.!\n",i,ForwardServer.auch_Uart2RxBuff[i]);
            // circle_buf_write(&ForwardServer.uart2RxCircleBuf, ForwardServer.auch_Uart2RxBuff[i]);

            if (circle_buf_write(&ForwardServer.Uart2RxCircleBuf, ForwardServer.auch_Uart2RxBuff[i]) != 0)
            {
                printf("Write failed at index %u with value 0x%02X\n", i, ForwardServer.auch_Uart2RxBuff[i]);
            }
        }
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket2Uart, ForwardServer.auch_Uart2RxBuff, BUF_SIZE);
    }

    else if (pst_UartStruct == ForwardServer.pst_Socket3Uart)
    {
        //ForwardServer.UartToSocket3Flag = 1;
        //ForwardServer.uin_Socket3TxSize = uin_Size;

        /*重新开启串口接收*/
        //STM32UartReceive(ForwardServer.pst_Socket3Uart, ForwardServer.auch_Socket3TxBuff, BUF_SIZE);

        for (uint16_t i = 0; i < uin_Size; i++)
        {
            // printf("start write circle,the  %u time val is %c.!\n",i,ForwardServer.auch_Uart2RxBuff[i]);
            // circle_buf_write(&ForwardServer.uart2RxCircleBuf, ForwardServer.auch_Uart2RxBuff[i]);

            if (circle_buf_write(&ForwardServer.Uart3RxCircleBuf, ForwardServer.auch_Socket3RxBuff[i]) != 0)
            {
                printf("Write failed at index %u with value 0x%02X\n", i, ForwardServer.auch_Socket3RxBuff[i]);
            }
        }
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket3Uart, ForwardServer.auch_Socket3RxBuff, BUF_SIZE);
    }
}

/**
 * 函数功能：串口发送完成中断回调函数
 * 参    数：pst_UartStruct 串口结构体
 *           uin_Size 接收字节数
 * 返 回 值：无
 */
void UartTxCplCallback(struct _Uart_TypeDef *pst_UartStruct)
{
    if (pst_UartStruct == ForwardServer.pst_Socket0Uart)
    {
        /*作为RS485的串口切换到发送模式*/
        if (pst_UartStruct->pst_DEPort != NULL)
        {
            HAL_GPIO_WritePin(ForwardServer.pst_Socket0Uart->pst_DEPort, ForwardServer.pst_Socket0Uart->ul_DEPin, GPIO_PIN_RESET);
        }
    }

    else if (pst_UartStruct == ForwardServer.pst_Socket1Uart)
    {
        /*作为RS485的串口切换到发送模式*/
        if (pst_UartStruct->pst_DEPort != NULL)
        {
            HAL_GPIO_WritePin(ForwardServer.pst_Socket1Uart->pst_DEPort, ForwardServer.pst_Socket1Uart->ul_DEPin, GPIO_PIN_RESET);
        }
    }

    else if (pst_UartStruct == ForwardServer.pst_Socket2Uart)
    {
        /*作为RS485的串口切换到发送模式*/
        if (pst_UartStruct->pst_DEPort != NULL)
        {
            HAL_GPIO_WritePin(ForwardServer.pst_Socket2Uart->pst_DEPort, ForwardServer.pst_Socket2Uart->ul_DEPin, GPIO_PIN_RESET);
        }
    }
    else if (pst_UartStruct == ForwardServer.pst_Socket3Uart)
    {
        /*作为RS485的串口切换到发送模式*/
        if (pst_UartStruct->pst_DEPort != NULL)
        {
            HAL_GPIO_WritePin(ForwardServer.pst_Socket3Uart->pst_DEPort, ForwardServer.pst_Socket3Uart->ul_DEPin, GPIO_PIN_RESET);
        }
    }
}

/**
 * 函数功能：串口错误中断回调函数
 * 参    数：pst_UartStruct 串口结构体
 * 返 回 值：无
 */
void UartErrorCallback(struct _Uart_TypeDef *pst_UartStruct)
{
    if (pst_UartStruct == ForwardServer.pst_Socket0Uart)
    {
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket0Uart, ForwardServer.auch_Socket0TxBuff, BUF_SIZE);
    }
    else if (pst_UartStruct == ForwardServer.pst_Socket1Uart)
    {
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket1Uart, ForwardServer.auch_Socket1TxBuff, BUF_SIZE);
    }
    else if (pst_UartStruct == ForwardServer.pst_Socket2Uart)
    {
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket2Uart, ForwardServer.auch_Uart2RxBuff, BUF_SIZE);
    }
    else if (pst_UartStruct == ForwardServer.pst_Socket3Uart)
    {
        /*重新开启串口接收*/
        STM32UartReceive(ForwardServer.pst_Socket3Uart, ForwardServer.auch_Socket3TxBuff, BUF_SIZE);
    }
}

/**
 * 函数功能：解析TCP数据控制GPIO
 * 参    数：接收数据和长度
 * 返 回 值：无
 */
void TCP_ProcessCommand(uint8_t *rxBuffer, uint16_t len)
{
    printf("TCP_ProcessCommand Buffer is %s\n", rxBuffer);

    if (memcmp(rxBuffer, "ON1", 3) == 0)  // 控制 GPIO1 高电平
    {
        printf("PWM1 is %s\n", rxBuffer);
        HAL_GPIO_WritePin(PWM1_GPIO_Port, PWM1_Pin, GPIO_PIN_SET);
    }
    else if (memcmp(rxBuffer, "OFF1", 4) == 0)  // 控制 GPIO1 低电平
    {   printf("PWM1 is %s\n", rxBuffer);
        HAL_GPIO_WritePin(PWM1_GPIO_Port, PWM1_Pin, GPIO_PIN_RESET);
    }         
		if (memcmp(rxBuffer, "ON2", 3) == 0)  // 控制 GPIO1 高电平
    {
        printf("PWM2 is %s\n", rxBuffer);
        HAL_GPIO_WritePin(PWM2_GPIO_Port, PWM2_Pin, GPIO_PIN_SET);
    }
    else if (memcmp(rxBuffer, "OFF2", 4) == 0)  // 控制 GPIO1 低电平
    {
			  printf("PWM2 is %s\n", rxBuffer);
        HAL_GPIO_WritePin(PWM2_GPIO_Port, PWM2_Pin, GPIO_PIN_RESET);
    }
}
